package de.szut.Pong.GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwingField extends JPanel implements Field {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2818877177291544110L;
	private final int HORIZONTALOFFSET = 75;
	private final int VERTICALOFFSET = 150;
	private final int PANELWIDTH = 800;
	private final int PANELHEIGHT = 800;
	private final int PIXELMULTIPLICATOR = 10;
	private final Point BALLSIZE = new Point(1, 1);
	private final Point PADSIZE = new Point(1, 6);
	private final Font COUNTERFONT = new Font("Arial", Font.BOLD, 100);
	private final Font VICTORYLABELFONT = new Font("Arial", Font.BOLD, 60);
	private Point ballPosition;
	private Point leftPadPosition;
	private Point rightPadPosition;
	private JLabel leftCounterLabel;
	private JLabel rightCounterLabel;
	private JLabel victoryLabel;
	private int leftCounter;
	private int rightCounter;

	public SwingField() {
		setSize(PANELWIDTH, PANELHEIGHT);
		setLayout(null);
		leftPadPosition = new Point(0, 27);
		rightPadPosition = new Point(65, 27);
		ballPosition = new Point(32, 29);
		leftCounter = 0;
		rightCounter = 0;

		leftCounterLabel = new JLabel(String.valueOf(leftCounter));
		leftCounterLabel.setBounds(HORIZONTALOFFSET + 100,
				VERTICALOFFSET - 110, 125, 125);
		leftCounterLabel.setFont(COUNTERFONT);
		leftCounterLabel.setAlignmentX(LEFT_ALIGNMENT);
		add(leftCounterLabel);
		rightCounterLabel = new JLabel(String.valueOf(rightCounter));
		rightCounterLabel.setBounds(HORIZONTALOFFSET + 650 - 150,
				VERTICALOFFSET - 110, 125, 125);
		rightCounterLabel.setFont(COUNTERFONT);
		rightCounterLabel.setAlignmentX(RIGHT_ALIGNMENT);
		add(rightCounterLabel);
		victoryLabel = new JLabel("", JLabel.CENTER);
		victoryLabel.setBounds(HORIZONTALOFFSET + 25, VERTICALOFFSET + 200, 600, 200);
		victoryLabel.setFont(VICTORYLABELFONT);
		victoryLabel.setOpaque(true);
		victoryLabel.setVisible(false);
		add(victoryLabel);
		

	}

	@Override
	public void moveBall(Point point) {
		ballPosition = point;
	}

	@Override
	public void moveLeftPad(PadMovement padMovement) {
		switch (padMovement) {

		case UP:
			leftPadPosition = new Point(leftPadPosition.getX(),
					leftPadPosition.getY() + 1);
			break;
		case DOWN:
			leftPadPosition = new Point(leftPadPosition.getX(),
					leftPadPosition.getY() - 1);
			break;
		case STOP:
			break;
		default:
			break;
		}

	}

	@Override
	public void moveRightPad(PadMovement padMovement) {
		switch (padMovement) {

		case UP:
			rightPadPosition = new Point(rightPadPosition.getX(),
					rightPadPosition.getY() + 1);
			break;
		case DOWN:
			rightPadPosition = new Point(rightPadPosition.getX(),
					rightPadPosition.getY() - 1);
			break;
		case STOP:
			break;
		default:
			break;
		}

	}

	@Override
	public void incrementLeftCounter() {
		leftCounter++;
		leftCounterLabel.setText(String.valueOf(leftCounter));

	}

	@Override
	public void incrementRightCounter() {
		rightCounter++;
		rightCounterLabel.setText(String.valueOf(rightCounter));

	}

	@Override
	public void displayHalfTime() {
		victoryLabel.setText("Halftime!");
		victoryLabel.setForeground(Color.BLACK);
		victoryLabel.setVisible(true);

	}

	@Override
	public void displayVictoryLeft() {
		victoryLabel.setText("Left Player won!");
		victoryLabel.setForeground(Color.GREEN);
		victoryLabel.setVisible(true);

	}

	@Override
	public void displayVictoryRight() {
		victoryLabel.setText("Right Player won!");
		victoryLabel.setForeground(Color.GREEN);
		victoryLabel.setVisible(true);

	}

	@Override
	public void displayDraw() {

	}

	@Override
	public void reset() {
		leftPadPosition = new Point(0, 27);
		rightPadPosition = new Point(65, 27);
		ballPosition = new Point(32, 29);
		leftCounter = 0;
		rightCounter = 0;
		leftCounterLabel.setText(String.valueOf(leftCounter));
		rightCounterLabel.setText(String.valueOf(rightCounter));
		victoryLabel.setVisible(false);
		showMoves();

	}
	@Override
	public void respawn(Point point) {
		leftPadPosition = new Point(0, 27);
		rightPadPosition = new Point(65, 27);
		ballPosition = point;
		victoryLabel.setVisible(false);
		showMoves();
		
	}
	@Override
	public void showMoves() {
		getParent().repaint();
		
	}
	//TODO: ERROR SCHREIBEN!
	@Override
	public void needUsersError() {
		victoryLabel.setText("Please set AIs!");
		victoryLabel.setForeground(Color.RED);
		victoryLabel.setVisible(true);
		
	}

	@Override
	public void paintComponent(Graphics g) {
		// Mainpaintings
		g.setColor(new Color(0, 0, 0));
		g.fillRect(HORIZONTALOFFSET - 2, VERTICALOFFSET, 2, 600);
		g.fillRect(PANELWIDTH - HORIZONTALOFFSET, VERTICALOFFSET, 2, 600);
		g.fillRect(HORIZONTALOFFSET - 2, VERTICALOFFSET - 2, 654, 2);
		g.fillRect(HORIZONTALOFFSET - 2, PANELHEIGHT - 50, 654, 2);

		// LeftPad
		g.fillRect(HORIZONTALOFFSET + 1, (600 + VERTICALOFFSET)
				- (leftPadPosition.getY() * PIXELMULTIPLICATOR)
				- (PADSIZE.getY() * PIXELMULTIPLICATOR), PADSIZE.getX()
				* PIXELMULTIPLICATOR - 1, PADSIZE.getY() * PIXELMULTIPLICATOR);

		// RightPad
		g.fillRect(HORIZONTALOFFSET + 650 - (PADSIZE.getX() * PIXELMULTIPLICATOR),
				(600 + VERTICALOFFSET)
						- (rightPadPosition.getY() * PIXELMULTIPLICATOR)
						- (PADSIZE.getY() * PIXELMULTIPLICATOR), PADSIZE.getX()
						* PIXELMULTIPLICATOR - 1, PADSIZE.getY()
						* PIXELMULTIPLICATOR);

		// Ball
		g.fillOval(HORIZONTALOFFSET + ballPosition.getX() * PIXELMULTIPLICATOR,
				(600 + VERTICALOFFSET) - (ballPosition.getY() * PIXELMULTIPLICATOR)
						- (BALLSIZE.getY() * PIXELMULTIPLICATOR), BALLSIZE.getX()
						* PIXELMULTIPLICATOR, BALLSIZE.getY() * PIXELMULTIPLICATOR);

	}




}
